﻿Imports WinForms = System.Windows.Forms

Imports System.ServiceModel
Imports System.ComponentModel

'Public Class MainForm
'    Private stopMenu As MenuItem = Nothing
'End Class


Class MainWindow






    ' Fields
    ' Private components As IContainer = Nothing
    Private host As ServiceHost
    '  Private serviceTrayIcon As WinForms.NotifyIcon
    'Private startMenu As MenuItem
    'Private stopMenu As MenuItem
    'Private menu As ContextMenu


    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

     


        'AddHandler Me.serviceTrayIcon.MouseDown, New WinForms.MouseEventHandler(AddressOf serviceTrayIcon_MouseDown)

        'serviceTrayIcon.Icon = My.Resources.serviceunknown

        'serviceTrayIcon.Visible = True


        Me.InitializeServiceHost()
        '   Me.InitializeContextMenu()


    End Sub





    'Sub serviceTrayIcon_MouseDown(ByVal sender As Object, ByVal e As WinForms.MouseEventArgs)



    '    If e.Button = WinForms.MouseButtons.Right Then



    '        Dim menu = CType(Me.FindResource("NotifierContextMenu"), ContextMenu)

    '        'Dim m As New ContextMenu

    '        'Dim mi As New MenuItem
    '        'mi.Icon = My.Resources.servicerunning
    '        'mi.Header = "Test"

    '        'menu.Items.Add(mi)


    '        menu.IsOpen = True

    '    End If


    'End Sub


    'Private Sub InitializeContextMenu()
    '    Dim list As New List(Of MenuItem)

    '    Dim startimg = New Image() With {.Source = My.Resources.servicerunning}
    '    Me.startMenu = New MenuItem() With {.Header = "Start service"}
    '    AddHandler Me.startMenu.Click, New EventHandler(AddressOf Me.startMenu_Click)
    '    Me.stopMenu = New MenuItem("Stop service")
    '    AddHandler Me.stopMenu.Click, New EventHandler(AddressOf Me.stopMenu_Click)
    '    Dim exitMenu As New MenuItem("E&xit")
    '    AddHandler exitMenu.Click, New EventHandler(AddressOf Me.exitMenu_Click)
    '    list.Add(Me.startMenu)
    '    list.Add(Me.stopMenu)
    '    list.Add(New MenuItem("-"))
    '    list.Add(exitMenu)
    '    Me.serviceTrayIcon.ContextMenu = New ContextMenu(list.ToArray)
    'End Sub

    Private Sub InitializeServiceHost()
        '服务的类
        Me.host = New ServiceHost(GetType(SonOVB.Samples.ComicSyndication.ComicService))
        AddHandler Me.host.Opened, New EventHandler(AddressOf Me.OnServiceHostOpened)
        AddHandler Me.host.Opening, New EventHandler(AddressOf Me.OnServiceHostOpening)
        AddHandler Me.host.Closed, New EventHandler(AddressOf Me.OnServiceHostClosed)
    End Sub





    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Loaded
        Me.host.Open()
    End Sub

    Private Sub OnServiceHostClosed(ByVal sender As Object, ByVal e As EventArgs)
        Me.Start.IsEnabled = True
        Me.Stop.IsEnabled = False
        Me.Title = "Service is stopped"

        'Me.serviceTrayIcon.ShowBalloonTip(3, "WinHost", Me.serviceTrayIcon.Text, ToolTipIcon.Info)
        'Me.serviceTrayIcon.Icon = My.Resources.servicestopped

        ShowImg(states.Stoped)

    End Sub

    Private Sub OnServiceHostOpened(ByVal sender As Object, ByVal e As EventArgs)
        Me.Start.IsEnabled = False
        Me.Stop.IsEnabled = True
        Me.Title = "Service is running"
        'Me.serviceTrayIcon.ShowBalloonTip(3, "WinHost", Me.serviceTrayIcon.Text, ToolTipIcon.Info)
        'Me.serviceTrayIcon.Icon = My.Resources.servicerunning
        ShowImg(states.Running)
    End Sub

    Private Sub OnServiceHostOpening(ByVal sender As Object, ByVal e As EventArgs)
        Me.Start.IsEnabled = False
        Me.Stop.IsEnabled = False
        Me.Title = "Service is opening"
        'Me.serviceTrayIcon.ShowBalloonTip(3, "WinHost", Me.serviceTrayIcon.Text, ToolTipIcon.Info)
        'Me.serviceTrayIcon.Icon = My.Resources.serviceunknown
        ShowImg(states.Unknow)
    End Sub




    Private Sub Start_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Start.Click
        Try
            Me.InitializeServiceHost()
            Me.host.Open()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Stop_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles [Stop].Click
        Try
            Me.host.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Exit_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles [Exit].Click
        If (Me.host.State <> CommunicationState.Closed) Then
            Me.host.Close()
        End If
        MyBase.Close()
    End Sub


    Enum states
        Unknow
        Running
        Stoped
    End Enum

    Private Sub ShowImg(ByVal state As states)
        Select Case state
            Case states.Running
                Me.imgError.Visibility = Windows.Visibility.Hidden
                Me.imgRunning.Visibility = Windows.Visibility.Visible
                Me.imgUnKnow.Visibility = Windows.Visibility.Hidden

            Case states.Stoped
                Me.imgError.Visibility = Windows.Visibility.Visible
                Me.imgRunning.Visibility = Windows.Visibility.Hidden
                Me.imgUnKnow.Visibility = Windows.Visibility.Hidden
            Case Else
                Me.imgError.Visibility = Windows.Visibility.Hidden
                Me.imgRunning.Visibility = Windows.Visibility.Hidden
                Me.imgUnKnow.Visibility = Windows.Visibility.Visible

        End Select
    End Sub

End Class
